<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>api自动生成</span>
      </div>
      <div>
        <div>
          <el-input
            type="textarea"
            :rows="5"
            placeholder="swagger.json "
            v-model="input"
          ></el-input>
           <br />
           <br />
          <el-input
            type="text"
            placeholder="moduleName "
            v-model="moduleName"
          ></el-input>
        </div>
        <br />
        <el-button type="primary" size="mini" @click="exchange"
          >转换</el-button
        >
        <br />
        <br />
        <div>
          <el-input
            type="textarea"
            :rows="5"
            placeholder="api "
            v-model="output"
          ></el-input>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  components: {  },
  name: "workflow-simulation",
  props: {},
  computed: {},
  watch: {},
  data() {
    return {
      input: "",
      output: "",
      moduleName: "",
      templates: {
        get:
          "{{methodname}}({commit,state},params){" +
          "return request({" +
          "url: '{{path}}' ," +
          "method: 'get'," +
          "params:params" +
          "})" +
          "}",
        delete:
          "{{methodname}}({commit,state},params){" +
          "return request({" +
          "url: '{{path}}' ," +
          "method: 'delete'," +
          "params:params" +
          "})" +
          "}",
        post:
          "{{methodname}}({commit,state},data){" +
          "return request({" +
          "url: '{{path}}' ," +
          "method: 'post'," +
          "data:data" +
          "})" +
          "}",
        put:
          "{{methodname}}({commit,state},data){" +
          "return request({" +
          "url: '{{path}}' ," +
          "method: 'put'," +
          "data:data," +
          "params:{id:data.id}" +
          "})" +
          "}",
      },
    };
  },
  mounted() {},
  methods: {
    exchange() {
      debugger
      this.output = this.convert(this.input);
    },
    convert(swaggerjson) {
      var swaggerjsonObj = JSON.parse(swaggerjson);

      var paths = swaggerjsonObj.paths;
      var definitions = swaggerjsonObj.definitions;

      var result = this.analyze(paths, definitions);
      return result;
    },
    analyze(paths, definitions) {
      let servicesMap = [];
      for (let key in paths) {
        if (key == "/locate") continue;

        var path = paths[key];
        var method = Object.keys(path)[0];
        var service = { url: '/'+this.moduleName+key, method: method };
        servicesMap.push(service);
      }
      var result = [];
      for (var key in servicesMap) {
       var service = servicesMap[key];
       var tempalte = this.templates[service.method];
       var serviceNames = service.url.split('/');
       var methodname = serviceNames[serviceNames.length-1];
       methodname = methodname.replace(methodname[0],methodname[0].toLowerCase())
       var serviceStr = tempalte.replace('{{methodname}}',methodname).replace('{{path}}',service.url)


        result.push(serviceStr);
      }
      return result.join(",\n");
    },
  },
};
</script>

<style></style>
